#include "serialserver.h"
#include "ui_serialserver.h"

#include "databases.h"

#include <QDebug>



#include <QSqlTableModel>

SerialServer::SerialServer(QWidget *parent) :
    Login(parent),
    ui(new Ui::SerialServer)
{
    ui->setupUi(this);
    this->setWindowTitle("SerialServer");
    
    ConnectFunc();

    this->userinfoShow();

    this->configInfo();

    this->workInfo();


}


/*
    对工作模式的增删改
 */
void SerialServer::workInfo()
{
    workModel =new QSqlRelationalTableModel(this);

    workModel->setTable("workmode");
    workModel->select();
    ui->work_tableView->setModel(workModel);

    workModel->setEditStrategy(QSqlTableModel::OnManualSubmit);
}
//增加工作模式数据
void SerialServer::on_work_addBtn_clicked()
{
    if(Login::access == "2")
    {
    QSqlRecord record = workModel->record();
    int rowNum = workModel->rowCount();
    workModel->insertRecord(rowNum,record);
    }
}
//保存工作数据
void SerialServer::on_work_saveBtn_clicked()
{
    if(Login::access == "2")
    {
    workModel->submitAll();
    }
}
//删除所选行的数据
void SerialServer::on_work_delBtn_clicked()
{
    if(Login::access == "2")
    {
    QItemSelectionModel *selectedModel = ui->work_tableView->selectionModel();
    QModelIndexList indexList = selectedModel->selectedRows();

    for(int i = 0;i<indexList.size();i++)
    {
        workModel->removeRow(indexList[i].row());
    }
    }
}


/*
   对配置表的增删改操作

 */
//展示配置表的内容
void SerialServer::configInfo()
{
    configModel =new QSqlRelationalTableModel(this);

    configModel->setTable("configmsg");
    configModel->select();
    ui->config_tableView->setModel(configModel);

    configModel->setEditStrategy(QSqlTableModel::OnManualSubmit);

    //设置表中的表头
}

//增加配饰数据
void SerialServer::on_config_addBtn_clicked()
{
    if(Login::access == "2")
    {
    QSqlRecord record =configModel->record();
    //将空记录加入到 configModel模型中
    int rowNum = configModel->rowCount();
    configModel->insertRecord(rowNum,record);
}
}

//保存当前数据
void SerialServer::on_config_saveBtn_clicked()
{
    if(Login::access == "2"){
        configModel->submitAll();
}
}

// 删除所选行
void SerialServer::on_config_delBtn_clicked()
{
    if(Login::access == "2")
    {
    //获取所有的选中行
    QItemSelectionModel *selectedModel = ui->config_tableView->selectionModel();
    // 获取选中的行号
    QModelIndexList indexList = selectedModel->selectedRows();

    for(int i = 0;i<indexList.size();i++)
    {
        configModel->removeRow(indexList[i].row());
    }
    }
}




/*
    对用户表增删改查的操作
*/
//展示用户数据表
void SerialServer::userinfoShow()
{

    loginModel = new QSqlTableModel(this);

    loginModel->setTable("logininfo");
    loginModel->select();
    ui->user_tableView->setModel(loginModel);



    //设置表头
    loginModel->setHeaderData(0,Qt::Horizontal,"编号");
    loginModel->setHeaderData(1,Qt::Horizontal,"用户名");
    loginModel->setHeaderData(2,Qt::Horizontal,"密码");
    loginModel->setHeaderData(3,Qt::Horizontal,"权限等级");

    ui->user_tableView->setAlternatingRowColors(true);
    loginModel->setEditStrategy(QSqlTableModel::OnManualSubmit);


}

//增加用户
void SerialServer::on_addBtn_clicked()
{
    if(Login::access == "2")
    {

        //创建一个新的空记录
        QSqlRecord record = loginModel->record();

        //2、将空记录加入到 loginModel 模型中
        int newrow = loginModel->rowCount(); //计算出一共有多少行

        loginModel->insertRecord(newrow,record);
    }
}

//修改用户的账号和密码
void SerialServer::on_saveBtn_clicked()
{
    if(Login::access == "2")
    {
        loginModel->submitAll();
    }
}

//删除用户
void SerialServer::on_delBtn_clicked()
{
    if(Login::access == "2")
    {    // 获取所有的选中行
        QItemSelectionModel *selectedModel = ui->user_tableView->selectionModel();
        // 获取选中行的行号
        QModelIndexList indexList = selectedModel->selectedRows();

        for (int i = 0; i < indexList.size(); i++)
        {
            loginModel->removeRow(indexList[i].row());
        }

    }
}


void SerialServer::isRoot(QTableView* tab)
{
    if(Login::access == "2")
    {
        tab->setEditTriggers(QAbstractItemView::DoubleClicked);
    }
    else {
        tab->setEditTriggers(QAbstractItemView::NoEditTriggers);
    }
}









void SerialServer::ConnectFunc()
{
    connect(ui->btn1,&QPushButton::clicked,this,[=](){SwitchPage();});
    connect(ui->btn2,&QPushButton::clicked,this,[=](){SwitchPage();});
    connect(ui->btn3,&QPushButton::clicked,this,[=](){SwitchPage();});
}

void SerialServer::SwitchPage()
{
    QPushButton *button = qobject_cast<QPushButton*>(sender());
    if(button==ui->btn1)
        ui->stackedWidget->setCurrentIndex(0);
    this->isRoot(ui->user_tableView);

    if(button==ui->btn2)
        ui->stackedWidget->setCurrentIndex(1);
    this->isRoot(ui->config_tableView);

    if(button==ui->btn3)
        ui->stackedWidget->setCurrentIndex(2);
    this->isRoot(ui->work_tableView);

    int i = 0;
    ui->stackedWidget->widget(i);
}

SerialServer::~SerialServer()
{
    delete ui;
}














